package com.liuxinlong.jobs;

import com.liuxinlong.common.SnowFlake;
import com.liuxinlong.constant.TimeFormatConstants;
import com.liuxinlong.enums.InventoryStatusEnum;
import com.liuxinlong.enums.MatterSourceEnum;
import com.liuxinlong.modules.dao.InventoryDao;
import com.liuxinlong.modules.dao.RoleRightDao;
import com.liuxinlong.modules.dao.UserMatterDao;
import com.liuxinlong.modules.entity.Inventory;
import com.liuxinlong.modules.entity.RoleRight;
import com.liuxinlong.modules.entity.UserMatter;
import com.liuxinlong.utils.ObjectUtils;
import com.liuxinlong.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 验证库存时间
 */
@Service
@Slf4j
@Component("checkInventoryTask")
public class CheckInventoryTask {

    @Autowired
    private InventoryDao inventoryDao;

    @Autowired
    private UserMatterDao userMatterDao;

    @Autowired
    private RoleRightDao roleRightDao;

    /**
     * 业务逻辑
     */
    public void reptilian(){
        log.info("checkInventoryJob start...");
        List<Inventory> inventoryList = inventoryDao.queryAvailableInfo();
        String currentTime = TimeUtils.getDate(TimeFormatConstants.YYYY_MM_DD);
        List<Inventory> updateList = new ArrayList<>();
        for (Inventory inventory: inventoryList) {
            String expireTime = inventory.getExpireTime();
            if(StringUtils.isEmpty(expireTime)) {
                continue;
            }
            float gapDay = TimeUtils.calculateTimeGapDay(currentTime,expireTime,TimeFormatConstants.YYYY_MM_DD);
            if (gapDay> 30){
                continue;
             }else if (gapDay < 0){
                if (inventory.getStatus() == InventoryStatusEnum.EXPIRED.getValue() || inventory.getStatus() == InventoryStatusEnum.PROCESSED.getValue()) {
                    continue;
                }
                inventory.setStatus(InventoryStatusEnum.EXPIRED.getValue());
                updateList.add(inventory);
            } else {
                if (inventory.getStatus() == InventoryStatusEnum.ABOUT_TO_EXPIRE.getValue() || inventory.getStatus() == InventoryStatusEnum.PROCESSED.getValue()) {
                    continue;
                }
                inventory.setStatus(InventoryStatusEnum.ABOUT_TO_EXPIRE.getValue());
                updateList.add(inventory);
            }
        }
        if (!ObjectUtils.isEmpty(updateList)) {
            inventoryDao.updateBatchById(updateList);
            List<RoleRight> roleRightList = roleRightDao.queryRoleByMenu("1003");
            for(RoleRight right: roleRightList) {
                UserMatter userMatter = new UserMatter();
                userMatter.setId(SnowFlake.nextIdStr());
                userMatter.setPendingRole(right.getRole());
                userMatter.setCreateTime(TimeUtils.getDate(TimeFormatConstants.YYYY_MM_DD_HH_MM_SS));
                userMatter.setSourceType(MatterSourceEnum.INVENTORY.getValue());
                userMatter.setSourceId(updateList.get(0).getId());
                userMatter.setContent("存在即将过期或已过期商品,请及时处理");
                userMatterDao.insert(userMatter);
            }

        }
        log.info("checkInventoryJob end...");
    }
}

